﻿using System;
using System.Linq;
using OSCSchema;

namespace OSC.Base
{
    public class PersonConverter : IConverter<IPerson, personType>
    {
        public personType Convert(IPerson p)
        {
            return new personType
                       {
                           userID = p.UserId,
                           address = p.Address,
                           anniversary = p.Anniversary.HasValue ? p.Anniversary.Value : DateTime.MinValue,
                           anniversarySpecified = p.Anniversary.HasValue,
                           birthday = p.Birthday.HasValue ? p.Birthday.Value : DateTime.MinValue,
                           birthdaySpecified = p.Birthday.HasValue,
                           cell = p.Cell,
                           city = p.Cell,
                           company = p.Company,
                           countryOrRegion = p.CountryOrRegion,
                           creationTime = p.CreationTime.HasValue ? p.CreationTime.Value : DateTime.MinValue,
                           creationTimeSpecified = p.CreationTime.HasValue,
                           emailAddress = p.EmailAddresses != null ? p.EmailAddresses.FirstOrDefault() : string.Empty,
                           emailAddress2 = p.EmailAddresses != null ? p.EmailAddresses.Skip(1).FirstOrDefault() : string.Empty,
                           emailAddress3 = p.EmailAddresses != null ? p.EmailAddresses.Skip(2).FirstOrDefault() : string.Empty,
                           expirationTime = p.ExpirationTime.HasValue ? p.ExpirationTime.Value : DateTime.MinValue,
                           expirationTimeSpecified = p.ExpirationTime.HasValue,
                           fileAs = p.FileAs,
                           firstName = p.FirstName,
                           friendStatus = p.FriendStatus,
                           fullName = p.FullName,
                           gender = p.Gender,
                           homePhone = p.HomePhone,
                           index = p.Index.HasValue ? p.Index.Value : 0,
                           indexSpecified = p.Index.HasValue,
                           lastModificationTime =
                           p.LastModificationTime.HasValue ? p.LastModificationTime.Value : DateTime.MinValue,
                           lastModificationTimeSpecified = p.LastModificationTime.HasValue,
                           lastName = p.LastName,
                           nickname = p.Nickname,
                           phone = p.Phone,
                           pictureUrl = p.PictureUrl,
                           relationship = p.Relationship,
                           state = p.State,
                           title = p.Title,
                           webProfilePage = p.WebProfilePage,
                           workPhone = p.WorkPhone,
                           zip = p.Zip
                       };
        }
    }
    public class ActivityTemplateConverter : IConverter<IActivityTemplate, activityTemplateContainerType>
    {
        public activityTemplateContainerType Convert(IActivityTemplate entity)
        {
            return new activityTemplateContainerType
                       {
                           applicationID = entity.ApplicationId,
                           templateID = 1,
                           activityTemplate = new activityTemplateType
                                                  {
                                                      type = ConvertActivityType(entity.ActivityType),
                                                      title = entity.Title,
                                                      icon = entity.Icon
                                                  }
                       };
        }
        public activityTemplateTypeRestrictionType ConvertActivityType(ActivityTypes type)
        {
            switch (type)
            {
                case ActivityTypes.StatusUpdate:
                    return activityTemplateTypeRestrictionType.StatusUpdate;
                case ActivityTypes.Photo:
                    return activityTemplateTypeRestrictionType.Photo;
                case ActivityTypes.Document:
                    return activityTemplateTypeRestrictionType.Document;
                default:
                    return activityTemplateTypeRestrictionType.Other;
            }
        }
    }
    public class ActivityConverter : IConverter<Activity, activityDetailsType>
    {
        public activityDetailsType Convert(Activity entity)
        {
            return new activityDetailsType
            {
                applicationID = entity.ApplicationId,
                objectID = entity.Id,
                ownerID = entity.Owner.UserId,
                publishDate = entity.PublishDate,
                templateID = 1,
                type = ConvertActivityType(entity.ActivityType),
                typeSpecified = entity.ActivityTypeSpecified,
                templateVariables = entity.Details.Select(
                        detail => detail.ToTemplateVariableType()
                ).ToArray()
            };
        }

        public activityTemplateTypeRestrictionType ConvertActivityType(ActivityTypes type)
        {
            switch (type)
            {
                case ActivityTypes.StatusUpdate:
                    return activityTemplateTypeRestrictionType.StatusUpdate;
                case ActivityTypes.Photo:
                    return activityTemplateTypeRestrictionType.Photo;
                case ActivityTypes.Document:
                    return activityTemplateTypeRestrictionType.Document;
                default:
                    return activityTemplateTypeRestrictionType.Other;
            }
        }
    }
}